﻿<%@ WebHandler Language="C#" Class="Shopping_Cart_SeveToSessionState" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Collections;
using System.Text;



public class Shopping_Cart_SeveToSessionState : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        /* 外部数据接收 */
        int operating_typ = Convert.ToInt32(context.Request.Params["operating_typ"]);
        int item_id = Convert.ToInt32(context.Request.Params["item_id"]);
        string html_code = context.Request.Params["shopping_cart_html"];
        html_code = html_code.Replace("\n", "").Replace("\"", "'");

        

        /* 数组定义 */
        ArrayList Shopping_Cart_List = new ArrayList();
        ArrayList Shopping_Cart_Html = new ArrayList();

        if (context.Session["Shopping_Cart_List"] != null && context.Session["Shopping_Cart_Html"] != null)
        {
            Shopping_Cart_List = (ArrayList)context.Session["Shopping_Cart_List"];
            Shopping_Cart_Html = (ArrayList)context.Session["Shopping_Cart_Html"];
        }

        /* 首先检测定位当前元素的ARRYLIST的位置 */
        int item_index = Shopping_Cart_List.IndexOf(item_id);
        /* 操作选择，1添加，0 删除 */
        if (operating_typ == 1)
        {
            if (item_index == -1)
            {
                Shopping_Cart_List.Add(item_id);
                Shopping_Cart_Html.Add(html_code);

                /* 存储到Session */
                context.Session["Shopping_Cart_List"] = Shopping_Cart_List;
                context.Session["Shopping_Cart_Html"] = Shopping_Cart_Html;

                Utils.ShowEnd(context, html_code);
            }
        }
        else if (operating_typ == 0)
        {
            if (item_index > -1)
            {
                Shopping_Cart_List.Remove(item_id);
                Shopping_Cart_Html.RemoveAt(item_index);

                /* 存储到Session */
                context.Session["Shopping_Cart_List"] = Shopping_Cart_List;
                context.Session["Shopping_Cart_Html"] = Shopping_Cart_Html;

                Utils.ShowEnd(context, html_code);
            }

        }
        /* 未执行任何操作 */
        Utils.ShowEnd(context, -1);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}